import React from 'react';
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
// import { WeaCheckbox, WeaDialog, WeaTab, WeaRightMenu } from "ecCom";
// import { Button, Modal } from "antd";
import { WeaDocTransportList } from '../../public';


const data = [
   {
      fileid: '0',
      filename: 'Nginx安装.mp4',
      filesize: '500MB',
      path: [],
      status: 'active',
      percent: 35,
   },
   {
      fileid: '1',
      filename: '上海市人民政府、上海警备区关于表彰2018年度上海市征兵工作先进单位和先进个人的决定.docx',
      filesize: '500MB',
      path: [{ id: '5x', text: '本地磁盘C' }],
      status: 'success',
   },
   {
      fileid: '2',
      filename: '国务院办公厅关于云南大理经济开发区升级为国家级经济技术开发区的复函.txt',
      filesize: '500MB',
      path: [{ id: '6x', text: '本地磁盘D本地磁盘D本地磁盘D本地磁盘D本地磁盘D本地磁盘D' }],
      status: 'success',
   },
   {
      fileid: '3',
      filename: 'JavaScript设计精髓.pdf',
      filesize: '500MB',
      path: [{ id: '6x', text: '本地磁盘D' }, { id: '7x', text: '软件目录' }],
      status: 'success',
   },
   {
      fileid: '4',
      filename: '卖桃者说.mp3',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '5',
      filename: 'error大礼包.zip',
      filesize: '500MB',
      path: [{ id: '6x', text: '本地磁盘D' }, { id: '7x', text: '软件目录' }, { id: '8x', text: '新建文件夹' }, { id: '9x', text: '秘密' }],
      status: 'success',
   },
   {
      fileid: '6',
      filename: 'index.html',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '7',
      filename: '你好**.jpg',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '8',
      filename: 'Excel Sheet.xlsx',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '9',
      filename: 'hello.ppt',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '10',
      filename: 'Nginx安装.mp4',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
   {
      fileid: '11',
      filename: 'Nginx安装.mp4',
      filesize: '500MB',
      path: [],
      status: 'success',
   },
];

let count = 0;

@inject('netDiskStore')
@observer
class Transportation extends React.Component {
   componentDidMount() {
      const { setTransportData } = this.props.netDiskStore;
      setTransportData({ ref: this.transportRef });
   }

   // 删除历史记录
   removeHistory = (id) => {
      // todo 调用API
      console.log('删除历史', id);
      return true;
   };

   // 点击目录名称 跳转到对应目录
   clickFolder = (path) => {
      const { pathSearch: { setPathSearchData } } = this.props.netDiskStore;
      setPathSearchData({ path });
   };

   // 上传开始
   startUpload = (a, b) => {
      console.log('上传开始', a, b);
   };

   // 上传成功
   uploadSuccess = (rewData, advice) => {
      console.log('上传成功', rewData, advice);
      const { fileSystem: { newUploadFile } } = this.props.netDiskStore;


      return new Promise((resolve, reject) => {
         // todo 调两个API：上传API，历史记录API
         // todo 将新文件添加到页面中
         let res = true; // api调用结果
         count++;
         count < 5 && (res = false);
         count >= 5 && (res = true);
         if (res) {
            resolve();
            let pathLen = advice.path.length;
            let fileData = {
               id: advice.fileid,
               type: 'file',
               textname: advice.filename,
               parentid: pathLen === 0 ? 'home' : advice.path[pathLen - 1].id,
               uptime: advice.datetime,
               titleText: { icon: `名称：${advice.filename}<br/>大小：${advice.filesize}`, name: advice.filename },
               ctrlX: false,
            };
            newUploadFile(fileData);
         } else {
            reject();
         }


         resolve();
      });
   };

   // 取消上传
   cancelUpload = (rawData, advice) => {
      console.log('业务组件 取消上传', rawData, advice);
   };


   // 上传错误
   uploadError = (data) => {
      console.log('上传错误', data);
   };


   render() {
      const { pathSearch } = this.props.netDiskStore;

      return (
         <WeaDocTransportList ecId={`${this && this.props && this.props.ecId || ''}_WeaDocTransportList@9yalpw`}
           ref={(e) => {
 this.transportRef = e; 
}}
           nowPath={toJS(pathSearch.path)}
           historyData={data}
           canJump={true}
           clickFolder={this.clickFolder}
           removeHistory={this.removeHistory}
           uploadSuccess={this.uploadSuccess}
           startUpload={this.startUpload}
           cancelUpload={this.cancelUpload}
           uploadError={this.uploadError}
         />
      );
   }
}

export default Transportation;

